package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.fenxiao.entity.WxCpUser;
import com.flyco.modules.stm.entity.StmSales;
import com.flyco.modules.stm.mapper.StmSalesMapper;
import com.flyco.modules.stm.service.IStmSalesService;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 导购
 * @Author: flyco
 * @Date:   2020-09-10
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmSalesServiceImpl extends ServiceImpl<StmSalesMapper, StmSales> implements IStmSalesService {

    @Autowired
    private StmSalesMapper stmSalesMapper;

    @Override
    public void saveOrUpdateFromWxCpUser(WxCpUser user) {
        List<StmSales> salesList = stmSalesMapper.getSalesByUserId(user.getUserId());
        if (oConvertUtils.isNotEmpty(salesList) && salesList.size() > 0) {
            List<Long> idList = salesList.stream().filter(s-> s.getDelFlag()==1).map(e -> e.getId()).collect(Collectors.toList());
            if (oConvertUtils.isNotEmpty(idList) && idList.size() > 0) {
                String ids = Joiner.on(",").join(idList);
                stmSalesMapper.updateDelFlagById(ids);
            }
        } else {
            stmSalesMapper.saveSalesByWxCpUser(user);
        }
    }

}
